﻿GY.Bill.SmallBillOffer = {
    fnShade: function (shade)
    {
        var height = $(document).height();
        $(shade).height(height);
    },
    countDown:function(time, obj)
    {
        var start_time = new Date(time).getTime(),
        sys_second = (new Date().getTime() - start_time) / 1000;
        var timer = setInterval(function () {
            if (sys_second < 24 * 60 * 60) {
                sys_second += 1;
                var day = Math.floor((sys_second / 3600) / 24);
                var hour = Math.floor((sys_second / 3600) % 24);
                var minute = Math.floor((sys_second / 60) % 60);
                var second = Math.floor(sys_second % 60);
                //$(obj).children(":eq(0)").text(day<10?"0"+day:day);//计算天
                $(obj).find("span").eq(0).text(hour < 10 ? "0" + hour : hour);//计算小时
                $(obj).find("span").eq(1).text(minute < 10 ? "0" + minute : minute);//计算分
                $(obj).find("span").eq(2).text(second < 10 ? "0" + second : second);// 计算秒
            } else {
                clearInterval(timer);
                $(obj).find("span").eq(0).text("24");//计算小时
                $(obj).find("span").eq(1).text("00");//计算分
                $(obj).find("span").eq(2).text("00");// 计算秒
            }
        }, 1000);
    },
    //初始化报价弹窗
    AddfnShade:function(xiaopiaoid,inviteid)
    {
        $("#hidBillOfferId").val(0);
        $("#hidXiaoPiaoId").val(xiaopiaoid);
        $("#hidInviteId").val(inviteid);
        
        $(".buymethodclass").removeClass("active");
        $(".buymethodclass").eq(0).addClass("active");

        $("#txtOfferAmount").val("");
        $("#txtMonthInterest").val("");
        $("#txtMonthInterest2").val("");
        $("#div_BillMethod").find("i").removeClass("active");
        $("#txtLocation").val("");
        $("#txtRemark").val("");
        
    },
    EditfnShade: function (billofferid, inviteid) {
        $("#hidBillOfferId").val(billofferid);
        $.ajax({
            type: "Post",
            url: $("#divMain").attr("data-offerdetailurl") + "/" + billofferid,
            success: function (result) {
                var data = result.data;
                if (data != null) {
                    $("#hidBillOfferId").val(data.Id);
                    $("#hidXiaoPiaoId").val(data.BillId);
                    $("#hidInviteId").val(inviteid);

                    $(".buymethodclass").removeClass("active");
                    if (data.OfferType == 1) {
                        //月息
                        $(".buymethodclass").eq(0).addClass("active");
                        $("#txtOfferAmount").val(data.OfferAmount);
                        $("#txtMonthInterest").val(data.MonthInterest);
                    }
                    else if (data.OfferType == 2) {
                        //金额
                        $(".buymethodclass").eq(1).addClass("active");
                        $("#txtOfferAmount2").val(data.OfferAmount);
                    }

                    $("#div_BillMethod").find("i").removeClass("active");
                    if (data.OfferMethod == 1) {
                        //买断
                        $("#div_BillMethod i").eq(0).addClass("active");
                    }
                    else {
                        //直帖
                        $("#div_BillMethod i").eq(1).addClass("active");
                    }
                    $("#txtLocation").val(data.OfferLocation);
                    $("#txtRemark").val(data.Remark);
                }
            }
        });
    },
    ValidDataResult: function () {
        var msg = "";
        if ($(".buymethodclass.active").attr("data-val") == 1) {
            var amount = $("#txtOfferAmount").val();
            var monthInterest = $("#txtMonthInterest").val();
            if (amount == "" || (amount / 1 <= 0)) {
                msg = "请输入正确的金额";
                return msg;
            }
            if (monthInterest == "" || (monthInterest / 1 <= 0)) {
                msg = "请输入正确的月息";
                return msg;
            }
        }
        else {
            var amount = $("#txtOfferAmount2").val();
            if (amount == "" || (amount / 1 <= 0)) {
                msg = "请输入正确的金额";
                return msg;
            }
        }
        var location = $("#txtLocation").val();
        var billMethod = $("#div_BillMethod").find(".active").attr("data-val");
        if (billMethod == undefined) {
            msg = "请选择操作方式";
            return msg;
        }
        if (location == "")
        {
            msg = "请选择操作地点";
            return msg;
        }
        return msg;
    },
    
    //保存报价
    SaveOffer: function (id) {
        var returnUrl = $("#divMain").attr("data-returnurl");
        //报价类型1月息,2金额,3月息加金额
        var monthInterest,offerAmount;
        var offerType = $(".buymethodclass.active").attr("data-val");
        if (offerType == 1)
        {
            //月息
            monthInterest = $("#txtMonthInterest").val();
            offerAmount = $("#txtOfferAmount").val();
        }
        else if (offerType == 2)
        { //金额
            monthInterest = 0;
            offerAmount = $("#txtOfferAmount2").val();
        }
        
        var model = {
            OfferId: $("#hidBillOfferId").val(),
            BillType:3,
            BillId: $("#hidXiaoPiaoId").val(),
            MonthInterest: monthInterest,
            OfferAmount: offerAmount,
            OfferMethod: $("#div_BillMethod").find(".active").attr("data-val"),
            OfferLocation: $("#txtLocation").val(),
            Remark: $("#txtRemark").val(),
            OfferType: offerType,
            InviteId: $("#hidInviteId").val()
        }
        $.ajax({
            type: "Post",
            url: $("#divMain").attr("data-url"),
            data: model,
            success: function (result) {
                location.href = returnUrl ;
                //location.href = returnUrl + "/" + $("#hidGroupId").val();
            }
        });
    }

}


$(function () {
    //var times = new GY.Bill.SmallBillOffer.countDown("2015/6/1 12:00:00", ".countdown .time");
    if (showTime.trim() == "") {
        var times = new GY.Bill.Common.CountDown(addTime, ".countdown .time");
    }
    //弹窗
    $(".offermask").on("tap", ".close", function () {
        $(".shade").hide();
        $(".offermask").hide();
    });
    //点击报价弹窗（为新增或者修改）
    $(".wrap").on("tap", ".shot", function () {
        //判断是否是新增或者修改
        if ($(this).attr("data-billofferid") == 0) {
            var xiaopiaoid = $(this).attr("data-xiaopiaoid");
            var inviteid = $(this).attr("data-inviteid");
            GY.Bill.SmallBillOffer.AddfnShade(xiaopiaoid,inviteid);
        }
        else {
            //var xiaopiaoid = $(this).attr("data-xiaopiaoid");
            var billofferid = $(this).attr("data-billofferid");
            var inviteid=$(this).attr("data-inviteid");
            GY.Bill.SmallBillOffer.EditfnShade(billofferid,inviteid);
        }
        GY.Bill.SmallBillOffer.fnShade($(".shade"));
        $(".shade").show();
        $("#div_offermask_int").show();
    });
    
    $("#div_offermask_int").on("focus", "input", function () {
        $("#div_offermask_int").css("position", "relative");
        GY.Bill.SmallBillOffer.fnShade($(".shade"));
    });
    $("#div_offermask_int").on("blur", "input", function () {
        $("#div_offermask_int").css("position", "fixed");
        GY.Bill.SmallBillOffer.fnShade($(".shade"));
    });

    //保存
    $("#BtnSave").on("tap", function () {
        var msg= GY.Bill.SmallBillOffer.ValidDataResult();
        if (msg == "") {
            var flag = GY.Bill.Common.SaveCheck($(this));
            if (flag) {
                GY.Bill.SmallBillOffer.SaveOffer();
            }
        }
        else {
            ShowTip(msg);
            return;
        }
    });
    //修改用户信息并保存报价信息
    $("#btnSaveUserOffer").on("tap", function () {
        var flag = GY.Bill.Common.SaveUser();
        if (flag) {
            $("#BtnSave").attr("data-check", 1);
            GY.Bill.SmallBillOffer.SaveOffer();
        }
    });

    //选择月息方式
    $(".buymethodclass").on("tap", function () {
        $(".buymethodclass").removeClass("active");
        $(this).addClass("active");
    });

    
    //操作方式选择
    $("#div_BillMethod").on("tap", "label", function () {
        $(this).parent().find("i").removeClass("active");
        $(this).children("i").addClass("active");
    });
    
    $(".shade").on("tap", function () {
        $(".shade").hide();
        $("#div_offermask_int").hide();
    });
});
